<template>
    <div class="major-add">
        <el-form label-width="80px" ref="form" :model="form" :rules="rules">
            <el-form-item label="学生" prop="studentId">
                <el-select filterable v-model="form.studentId" disabled placeholder="请选择学生">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="课程" prop="courseId">
                <el-select filterable v-model="form.courseId" disabled placeholder="请选择课程">
                    <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="日常得分" prop="dailyScore">
                <el-input-number v-model="form.dailyScore" controls-position="right" :min="0"
                    :max="100"></el-input-number>
            </el-form-item>
            <el-form-item label="考试得分" prop="examScore">
                <el-input-number v-model="form.examScore" controls-position="right" :min="0"
                    :max="100"></el-input-number>
            </el-form-item>
            <el-form-item label="总分" prop="score">
                <el-input-number v-model="form.score" controls-position="right" :min="0" :max="200"></el-input-number>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer" style="display: flex; justify-content: flex-end;">
            <el-button type="primary" @click="onsubmit">确定</el-button>
            <el-button type="primary" @click="handclose" plain>取消</el-button>
        </span>
    </div>
</template>
<script>
import { coursenames, studentnames, adminstudentid, studentup } from '@/api/courseup';
export default {
    props: {
        id: { type: Number, default: null }
    },
    data() {
        return {
            form: { id: "", studentId: '', courseId: '', dailyScore: "", examScore: "", score: "" },
            options: [{
                value: '',
                label: ''
            }],
            options1: [{
                value: '',
                label: ''
            }],
            rules: {
                name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' }
                ],
                departmentId: [
                    { required: true, message: '请选择院系', trigger: 'change' }
                ],
                number: [
                    { required: true, message: '请输入工号', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
            }
        }
    },
    mounted() {
        studentnames().then(res => {
            console.log(res);
            this.options = (res.data || []).map(item => ({
                label: item.name,
                value: item.id
            }));
        })
        coursenames().then(res => {
            console.log(res);
            this.options1 = (res.data || []).map(item => ({
                label: item.name,
                value: item.id
            }));
        })
        if (this.id) {
            adminstudentid(this.id).then(res => {
                if (res.code === 0) {
                    console.log(res);
                    this.form = res.data;
                } else {
                    this.$message.error('获取专业信息失败');
                }
            });
        }
    },
    methods: {
        async onsubmit() {
            this.$refs["form"].validate(async flag => {
                if (flag) {
                    console.log(this.form)
                    const res = await studentup(this.form);
                    if (res.code == 0) {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                        });
                        this.$emit('done');
                    } else {
                        console.log(res);
                        this.$message({
                            message: '操作失败',
                            type: 'error',
                        });
                    }
                }
            })
        },
        handclose() {
            this.$emit('close')
        },
        handleChange(value) {
            console.log(value);
        }
    }
}
</script>
